<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="70px" label="文章标题" prop="articleId">
        <el-input
          v-model="queryParams.articleTitle"
          placeholder="请输入文章标题"
          clearable
          @clear="resetQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点评人" prop="userId">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入点评人姓名"
          clearable
          @clear="resetQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="80px" label="评论时间" prop="date">
        <el-date-picker
          clearable
          @clear="resetQuery"
          v-model="date"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['comment:comment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['comment:comment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-table v-loading="loading" :data="scope.row.replyList">
            <el-table-column label="id" width="100" align="center" prop="id" />
            <el-table-column label="回复人" width="100" align="center" prop="userId" >
              <template slot-scope="scope1">
                {{ scope1.row.sysUser.nickName }}
              </template>
            </el-table-column>
            <el-table-column label="回复内容" width="300" align="center" prop="content" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" width="100" align="center" prop="id" />
      <el-table-column label="文章标题" align="center" prop="articleId">
        <template slot-scope="scope">
         {{ scope.row.article.title }}
        </template>
      </el-table-column>
      <el-table-column label="点评人" width="100" align="center" prop="userId">
        <template slot-scope="scope">
         {{ scope.row.sysUser.nickName }}
        </template>
      </el-table-column>
      <el-table-column label="评论内容" align="center" prop="content" >
        <template slot-scope="scope">
           <div class="custom-height-cell">{{scope.row.content}}</div> <!-- 使用自定义类包裹单元格内容 -->
         </template>
      </el-table-column>
      <el-table-column label="评论时间" align="center" prop="date" />
      <el-table-column label="点赞量" align="center" prop="upvoteNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['comment:comment:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLook(scope.row)"
            v-hasPermi="['comment:comment:remove']"
          >查看评论内容</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 查看评价内容对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <div style="margin-bottom: 10px;">{{ this.form.content }}</div>
        <el-form :modal="reply">
           <el-form-item label="回复:">
             <el-input v-model="reply.content" type="textarea" :rows="2" placeholder="请输入回复内容"/>
           </el-form-item>
          <div class="dialog-footer">
            <el-button type="primary" @click="onSubmit">回复</el-button>
            <el-button @click="cancel">取消</el-button>
          </div>
        </el-form>


      </el-dialog>
  </div>
</template>


<style>
  .custom-height-cell {
    text-align: left;
    height: 90px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 4;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .dialog-footer {
    display: flex;
    justify-content: right;
  }
</style>


<script>
import { listComment, getComment, delComment, addComment, updateComment } from "@/api/comment/comment";
import { addReplay } from "@/api/replay/replay"
export default {
  name: "Comment",
  data() {
    return {
      // 回复内容集合
      replyList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章点评管理表格数据
      commentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      reply:{
        articleId: 0,
        userId: 0,
        content:'',
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleId: null,
        userId: null,
        date: null,
        articleTitle: null,
        nickName: null
      },
      date:'',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        articleId: [
          { required: true, message: "文章id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "评论内容不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "评论时间不能为空", trigger: "blur" }
        ],
        isDeleted: [
          { required: true, message: "逻辑删除不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  watch:{
    date(){
      if (this.date){
        this.queryParams.date = this.date[0] +"to"+ this.date[1]
      }else {
        this.queryParams.date = null
      }
    }
  },
  methods: {
    // 提交回复方法
    onSubmit(){
      addReplay(this.reply).then(respone=>{
        this.$modal.msgSuccess("回复成功");
        this.open = false;
        this.reply = {};
        this.getList();
      })

    },
    /** 查询文章点评管理列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(response)
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        articleId: null,
        userId: null,
        content: null,
        date: null,
        upvoteNum: null,
        isDeleted: null
      };
      this.reply = {}
      this.resetForm("form");
      this.resetForm("reply")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章点评管理";
    },
    // 查看评价内容按钮
    handleLook(row){
      this.reset();
      const id = row.id || this.ids
      getComment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "评价内容";
      });
      console.log(row)
      this.reply.commentId = row.id,
      this.reply.userId = row.userId
    },

    // /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateComment(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addComment(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文章管理编号为"' + ids + '"的数据项？').then(function() {
        return delComment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('comment/comment/export', {
        ...this.queryParams
      }, `comment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
